package com.zzty.txj1.controller;

import com.zzty.txj1.entity.DoEvent;
import com.zzty.txj1.entity.ResponseUser;
import com.zzty.txj1.entity.SearchDoEvent;
import com.zzty.txj1.entity.User;
import com.zzty.txj1.service.DoEventService;
import com.zzty.txj1.service.UserService;
import com.zzty.txj1.util.ResponseMessageUtil;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.util.*;

@RestController
@RequestMapping("/doEvent")
public class DoEventController {
    private static final Logger logger = LoggerFactory.getLogger(DoEventController.class);

    @Autowired
    DoEventService doEventService;
    @Autowired
    UserService userService;

    @RequestMapping(value = "/checkEvents", method = RequestMethod.POST)
    public List checkEvents(DoEvent doEvent) {
        logger.info("DoEvent--controller ============ checkEvents doEvent begin :" + doEvent.toString());
        if(doEvent.getDate()==null){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            Date zero = calendar.getTime();
            doEvent.setDate(zero);
        }
        logger.info("DoEvent--controller ============ checkEvents doEvent :" + doEvent.toString());
        List<DoEvent> doEvents = new ArrayList<>();
        doEvents = doEventService.checkEvents(doEvent);
        logger.info("DoEvent--controller ============ checkEvents doEvents are :" + doEvents.toString());
        return doEvents;
    }

    @RequestMapping(value = "/getUserByOpenId")
    public List<User> getUserByOpenId(HttpServletRequest request, User user) {
        logger.info("DoEvent--controller ============ getUserByOpenId openId :" + user.getOpenId());
        User user1 = userService.getUserByOpenId(user.getOpenId());
        request.getSession().setAttribute("user", user1);
        List<User> users = new ArrayList<>();
        users.add(user1);
       // logger.info("DoEvent--controller ============ getUserByOpenId users :" + user1.toString());
        return users;
    }

    @RequestMapping(value = "/getDoEventByDate")
    public List getDoEventByDate(SearchDoEvent searchDoEvent) {
        List<DoEvent> doEvents = doEventService.getDoEventByDate(searchDoEvent);
        logger.info("DoEvent--controller ============ getDoEventByDate doEvents :" + doEvents);
        return doEvents;
    }

    @RequestMapping(value = "/getNumsByUserIdAndEventId")
    public Integer getNumsByUserIdAndEventId(SearchDoEvent searchDoEvent) {
        Integer count = doEventService.getNumsByUserIdAndEventId(searchDoEvent);
        logger.info("DoEvent--controller ============ getNumsByUserIdAndEventId count :" + count);
        return count;
    }

    @RequestMapping(value = "/updateUserEvent", method = RequestMethod.POST)
    public Integer updateUserEvent(DoEvent doEvent) {
        Integer flag = doEventService.updateUserEvent(doEvent);
        logger.info("DoEvent--controller ============ updateUserEvent flag :" + flag);
        return flag;
    }

    @RequestMapping(value = "/updateUserEvent1", method = RequestMethod.POST)
    public Integer updateUserEvent1(DoEvent doEvent) {
        Integer flag = doEventService.updateUserEvent1(doEvent);
        logger.info("DoEvent--controller ============ updateUserEvent1 flag :" + flag);
        return flag;
    }

    @RequestMapping(value = "/updateUserEvent2", method = RequestMethod.POST)
    public Integer updateUserEvent2(DoEvent doEvent) {
        Integer flag = doEventService.updateUserEvent2(doEvent);
        logger.info("DoEvent--controller ============ updateUserEvent2 flag :" + flag);
        return flag;
    }

    @RequestMapping(value = "/updateDoEvent", method = RequestMethod.POST)
    public Integer updateDoEvent(DoEvent doEvent) {
        Integer flag = doEventService.updateDoEvent(doEvent);
        logger.info("DoEvent--controller ============ updateDoEvent flag :" + flag);
        return flag;
    }

    @RequestMapping(value = "/deleteDoEventById", method = RequestMethod.POST)
    public Integer deleteDoEventById(DoEvent doEvent) {
        Integer flag = doEventService.deleteDoEventById(doEvent);
        logger.info("DoEvent--controller ============ deleteDoEventById flag :" + flag);
        return flag;
    }

    @RequestMapping(value = "/getDoEventById")
    public Integer getDoEventById(DoEvent doEvent) {
        Integer result = null;
        DoEvent doEvent1 = doEventService.getDoEventById(doEvent);
        result = doEvent1.getResult();
        logger.info("DoEvent--controller ============ getDoEventById result :" + result);
        return result;
    }

    @RequestMapping("/getDoEvent")
    public Integer getDoEvent(DoEvent doEvent) {
        Integer result = null;
        DoEvent doEvent1 = doEventService.getDoEvent(doEvent);
        result = doEvent1.getResult();
        logger.info("DoEvent--controller ============ getDoEvent result :" + result);
        return result;
    }

    @RequestMapping(value = "/responseMessage", method = RequestMethod.POST)
    public Integer responseMessage(ResponseUser responseUser) throws Exception {
        ResponseMessageUtil responseMessageUtil = new ResponseMessageUtil();
        String result = responseMessageUtil.responseMessage(responseUser.getOpenId(), responseUser.getEventName());
        logger.info("DoEvent--controller ============ responseMessage result :" + result);
        Integer flag = 1;
        return flag;
    }


    @RequestMapping("/test/{id}")
    public DoEvent test(@PathParam("id") Integer id){
        DoEvent doEvent = new DoEvent();
        logger.info("DoEvent--controller ============ checkEvents doEvent begin :" + doEvent.toString());
        if(doEvent.getDate()==null){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            Date zero = calendar.getTime();
            doEvent.setDate(zero);
        }
        doEvent.setUserId(id);

        logger.info("DoEvent--controller ============ checkEvents doEvent :" + doEvent.toString());
        List<DoEvent> doEvents = new ArrayList<>();
        doEvents = doEventService.checkEvents(doEvent);
        logger.info("DoEvent--controller ============ checkEvents doEvents are :" + doEvents.toString());
        return doEvent;
    }

}
